using System;
using System.Text;

namespace InfoJetSoft.Service.Util
{
	/// <summary>
	/// Summary description for ExpressionUtil.
	/// </summary>
	public class ExpressionUtil
	{
		public static bool exprContainingNode( string expr, string nodeName ){
			char[] chars = expr.ToCharArray();
			int index = expr.IndexOf( nodeName, 0 );
			while( index >= 0 ){
				if( index + nodeName.Length < expr.Length ){
					char nextChar = chars[ index + nodeName.Length ];
					if( StringUtil.isXmlNameChar( nextChar ) ){
						index = expr.IndexOf( nodeName, index + nodeName.Length );
						continue;
					}else{
						return true;
					}
				}else{
					return true;
				}
			}
			return false;
		}

		//CID 0218 //2009.01.31 //Begin
		public static bool exprContainingDataSource( string expr, string dsName )
		{
			if( expr.IndexOf( "xdXDocument:GetDOM(\"" + dsName + "\")" ) >= 0 )
			{
				return true;
			}
			return false;
		}
		//CID 0218 //2009.01.31 //End

		public static string parseTargetNodeName( string path ){
            //CID 1013 //2011.08.31 //Begin
            //CID 0518 //2010.03.24 //Begin
            path = RemoveBrackets(path);
			int index = path.LastIndexOf("/");
            //CID 0518 //2010.03.24 //End
            //CID 1013 //2011.08.31 //End
			if( index > 0 )
            {
                //CID 0947 //2011.06.13 //Trim()
				return path.Substring( index + 1 ).Trim();
			}else{
				return path;
			}
		}

        //CID 1013 //2011.08.31 //Begin
        private static string RemoveBrackets(string path)
        {
            char[] chars = path.ToCharArray();
            StringBuilder buffer = new StringBuilder();
            int ignoring = 0;
            foreach (char chr in chars)
            {
                if (chr == '[')
                {
                    ignoring++;
                }
                else if (chr == ']')
                {
                    ignoring--;
                }
                else
                {
                    if (ignoring == 0)
                    {
                        buffer.Append(chr);
                    }
                }
            }
            return buffer.ToString();
        }
        //CID 1013 //2011.08.31 //End

		public static string Nz = "xdMath:Nz(";

		public static string formatExpression( string expression )
		{
			string expr = formatNz( expression );
			return expr;
		}

		private static string formatNz( string expression )
		{
			if( System.Environment.Version.Major >= 2 )
			{
				return expression;
			}
			if( expression.IndexOf( Nz ) < 0 )
			{
				return expression;
			}
			StringBuilder buffer = new StringBuilder( expression );
			int length = Nz.Length;
			int index = buffer.ToString().IndexOf( Nz );
			while( index >= 0 )
			{
				buffer.Remove( index, length );
				int backet = 0;
				for( int i = index; i < buffer.Length; i ++ )
				{
					char c = buffer[ i ];
					if( c == ')' )
					{
						if( backet == 0 )
						{
							buffer.Remove( i, 1 );
							break;
						}
						else
						{
							backet --;
						}
					}
					if( c == '(' )
					{
						backet ++;
					}
				}
				if( backet != 0 )
				{
					return expression;
				}
				index = buffer.ToString().IndexOf( Nz );
			}
			return buffer.ToString();
		}

	}
}
